<?php

namespace App\Admin\Controllers;

use App\Models\Novel;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class NovelController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('小说');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('详情');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {


            $content->header('新增');
            $content->description('详情');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Novel::class, function (Grid $grid) {

            $grid->id('id')->sortable();
            $grid->column('title', '书名');
            $grid->author('作者');
            $grid->reading_num('在读人数');
            $grid->cover('封面')->display(function($cover){
                return empty($cover)?'没有':'有';
            });
            $grid->is_recommend('推荐')->display(function($released){
                return $released == 1 ? '是':'否';
            });
            $grid->chapter('章节数')->display(function($chapter){
                $count = count($chapter);
                return $count;
            });
            $grid->sight('长评条数')->display(function($sight){
                $count = count($sight);
                return "<span class='label label-warning'>{$count}</span>";
            });

            $grid->created_at('创建时间');
            $grid->updated_at('修改时间');
            $grid->actions(function ($actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Novel::class, function (Form $form) {
            $form->image('cover','封面');
            $form->text('title', '标题');
            $form->text('author', '作者');
            $form->text('is_recommend', '1推荐，0不推荐')->setWidth(1,2);


        });
    }
}
